package com.jinyang.jetpackdemo

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.lifecycle.*
import com.jeremyliao.liveeventbus.LiveEventBus
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.activity.*
import com.jinyang.jetpackdemo.hilt.HiltMvvmActivity
import com.jinyang.jetpackdemo.paging3.Paging3Activity
import com.jinyang.jetpackdemo.util.*
import com.permissionx.guolindev.PermissionX
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : Activity(), LifecycleOwner {
    private lateinit var mLifecycleRegistry: LifecycleRegistry

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mLifecycleRegistry = LifecycleRegistry(this)
        mLifecycleRegistry.currentState = Lifecycle.State.CREATED
        LiveEventBus
            .get("key1", Int::class.java)
            .observe(this) { value: Int -> LjyLogUtil.d("MainActivity.value:$value") }
        lifecycleScope.launch(Dispatchers.IO) {
            startMethodTracingSampling()
        }
    }


    override fun onStart() {
        super.onStart()
        mLifecycleRegistry.currentState = Lifecycle.State.STARTED
    }

    override fun onResume() {
        super.onResume()
        mLifecycleRegistry.currentState = Lifecycle.State.RESUMED


    }

    override fun onPause() {
        super.onPause()
        mLifecycleRegistry.currentState = Lifecycle.State.STARTED
    }

    override fun onStop() {
        super.onStop()
        mLifecycleRegistry.currentState = Lifecycle.State.CREATED
    }

    override fun onDestroy() {
        super.onDestroy()
        mLifecycleRegistry.currentState = Lifecycle.State.DESTROYED
        stopMethodTracing()
    }

    override fun getLifecycle(): Lifecycle {
        return mLifecycleRegistry
    }


    @RequiresApi(Build.VERSION_CODES.M)
    fun onBtnCLick(view: View) {
        startMethodTracing()
        when (view.id) {
            R.id.btn_Lifecycle -> startActivity(Intent(this, LifecycleDemoActivity::class.java))
            R.id.btn_LiveData -> startActivity(Intent(this, LiveDataActivity::class.java))
            R.id.btn_ViewModel -> startActivity(Intent(this, ViewModelActivity::class.java))
            R.id.btn_DataBinding -> startActivity(Intent(this, ArticleListActivity::class.java))
            R.id.btn_DataBindingRecyclerView -> startActivity(
                Intent(
                    this,
                    ArticleList2Activity::class.java
                )
            )
            R.id.btn_Paging3 -> startActivity(Intent(this, Paging3Activity::class.java))
            R.id.btn_WorkManager -> startActivity(Intent(this, WorkManagerActivity::class.java))
            R.id.btn_DataStore -> startActivity(Intent(this, DataStoreActivity::class.java))
            R.id.btn_Hilt -> startActivity(Intent(this, HiltActivity::class.java))
            R.id.btn_HiltMVVM -> startActivity(Intent(this, HiltMvvmActivity::class.java))
            R.id.btn_navi -> startActivity(Intent(this, ConsultationServiceActivity::class.java))
            R.id.btn_RetrofitDemo -> startActivity(Intent(this, RetrofitDemoActivity::class.java))
            R.id.btn_RxjavaDemo -> startActivity(Intent(this, RxjavaDemoActivity::class.java))
            R.id.btn_GlideDemo -> startActivity(Intent(this, GlideDemoActivity::class.java))
            R.id.btn_EventBus -> startActivity(Intent(this, EventBusActivity::class.java))
            R.id.btn_ButterKnife -> startActivity(Intent(this, ButterKnifeActivity::class.java))
            R.id.btn_LeakCanary -> startActivity(Intent(this, LeakCanaryActivity::class.java))
            R.id.btn_ComposeDemo -> startActivity(Intent(this, ComposeDemoActivity::class.java))
            R.id.btn_Storage -> startActivity(Intent(this, StorageActivity::class.java))
            R.id.btn_XML -> startActivity(Intent(this, XmlParserTestActivity::class.java))
            R.id.btn_ServiceTest -> startActivity(Intent(this, ServiceTestActivity::class.java))
            R.id.btn_Coroutine -> startActivity(Intent(this, CoroutineActivity::class.java))
            R.id.btn_Handler -> startActivity(Intent(this, HandlerActivity::class.java))
            R.id.btn_BroadCast -> startActivity(Intent(this, BroadCastActivity::class.java))
            R.id.btn_Gson -> startActivity(Intent(this, GsonActivity::class.java))
            R.id.btn_Kotlins -> startActivity(Intent(this, KotlinsActivity::class.java))
            R.id.btn_AvatarList -> startActivity(Intent(this, AvatarListActivity::class.java))
            R.id.btn_Progress -> startActivity(Intent(this, ProgressActivity::class.java))
            R.id.btn_ClassLoader -> startActivity(Intent(this, ClassLoaderActivity::class.java))
            R.id.btn_View -> startActivity(Intent(this, ViewActivity::class.java))
            R.id.btn_WebShowPdf -> {
                val intent = Intent(this, WebShowPdfActivity::class.java)
                intent.putExtra("loadUrl", "https://fdfs.runpozx.com/group2/M00/0C/10/CmQVXWGXb0CAYZDvADn8LnXtb-s449.pdf")
//                intent.putExtra("loadUrl", "https://overseas-ins-app.runpozx.com/#/product-detail?insuranceCode=I0002501")
                startActivity(intent)
            }

            R.id.btn_Camerax -> {
                startActivity(Intent(this, CameraxActivity::class.java))
            }
            R.id.btn_OkHttpDemo -> {
                if (ActivityCompat.checkSelfPermission(
                        this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(
                        this,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    this@MainActivity.requestPermissions(
                        arrayOf(
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE
                        ), 10001
                    )
                } else {
                    startActivity(Intent(this, OkHttpDemoActivity::class.java))
                }
            }
        }
        stopMethodTracing()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 10001) {
            MyLiveData.get(this)?.observe(this, Observer {
                LjyLogUtil.d("LiveDataActivity:$it")
            })
        }
    }
}

class MyObserver2 : LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onCreate:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onStart(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onStart:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onResume:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onPause:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onStop(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onStop:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onDestroy:${owner.lifecycle.currentState}")
    }
}